package com.hbean.controller;

import com.hbean.entity.SysMenu;
import com.hbean.service.SysMenuService;
import com.hbean.system.annotation.LogMethod;
import com.hbean.system.config.Base;
import com.hbean.utils.HUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-10-27 21:53
 * @ModificationHistory:
 */

@Slf4j
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends Base{
    
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * description: 查询
     * create_user: hjibo
     * create_date: 2018-09-29 10:13
     * param :
     */
    @RequestMapping("/getSysMenus")
    public List<SysMenu> getSysMenus(HttpSession session){
        List<SysMenu> sysMenus = null;
        try {
            sysMenus = sysMenuService.getDataForCache();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysMenus;
    }

    /**
     * description: 查询
     * create_user: hjibo
     * create_date: 2018-09-29 10:13
     * param :
     */
    @RequestMapping("/getSysMenu")
    public SysMenu getSysMenu(HttpServletRequest request){
        SysMenu sysMenu = null;
        try {
            List<SysMenu> sysMenus = sysMenuService.getDataForCache();//从缓存中取数据
            sysMenus = sysMenus.stream().filter(sysMenu1 -> sysMenu1.getSm00().equals(request.getParameter("sm00"))).collect(Collectors.toList());
            sysMenu = sysMenus.get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysMenu;
    }

    /**
     * description: 新增
     * create_user: hjibo
     * create_date: 2018-09-29 10:19
     * param :
     */
    @RequestMapping("/addSysMenu")
    @LogMethod(logName = "新增菜单")
    public Map addSysMenu(HttpServletRequest request){
        try {
            SysMenu sysMenu = new SysMenu();
            if(HUtil.isNotEmpty(request.getParameter("sm01"))) sysMenu.setSm01(request.getParameter("sm01"));
            if(HUtil.isNotEmpty(request.getParameter("sm02"))) sysMenu.setSm02(request.getParameter("sm02"));
            if(HUtil.isNotEmpty(request.getParameter("sm04"))) sysMenu.setSm04(request.getParameter("sm04"));
            if(HUtil.isNotEmpty(request.getParameter("sm05"))) sysMenu.setSm05(request.getParameter("sm05"));
            if(HUtil.isNotEmpty(request.getParameter("sm06"))) sysMenu.setSm06(request.getParameter("sm06"));
            if(HUtil.isNotEmpty(request.getParameter("sm07"))) sysMenu.setSm07(request.getParameter("sm07"));
            if(HUtil.isNotEmpty(request.getParameter("sm10"))) sysMenu.setSm10(Integer.parseInt(request.getParameter("sm10")));

            this.sysMenuService.insertDataAndUpdateCache(sysMenu);

            return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
        } catch (Exception e) {
            log.info("保存菜单信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }

    /**
     * description: 删除
     * create_user: hjibo
     * create_date: 2018-09-29 15:50
     * param :
     */
    @RequestMapping("/removeSysMenu")
    @LogMethod(logName = "删除菜单")
    public Map removeSysMenu(HttpServletRequest request){
        try {
            SysMenu sysMenu = new SysMenu();
            if(HUtil.isNotEmpty(request.getParameter("sm00"))){
                sysMenu.setSm00(request.getParameter("sm00"));
                this.sysMenuService.deleteDataAndUpdateCache(sysMenu);
                return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
            }
        } catch (Exception e) {
            log.info("删除菜单信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }

    /**
     * description: 修改
     * create_user: hjibo
     * create_date: 2018-09-29 10:19
     * param :
     */
    @RequestMapping("/modSysMenu")
    @LogMethod(logName = "修改菜单")
    public Map modSysMenu(HttpServletRequest request){

        try {
            SysMenu sysMenu = new SysMenu();

            if(HUtil.isNotEmpty(request.getParameter("sm00"))) sysMenu.setSm00(request.getParameter("sm00"));
            if(HUtil.isNotEmpty(request.getParameter("sm01"))) sysMenu.setSm01(request.getParameter("sm01"));
            if(HUtil.isNotEmpty(request.getParameter("sm05"))) sysMenu.setSm05(request.getParameter("sm05"));
            if(HUtil.isNotEmpty(request.getParameter("sm06"))) sysMenu.setSm06(request.getParameter("sm06"));
            if(HUtil.isNotEmpty(request.getParameter("sm07"))) sysMenu.setSm07(request.getParameter("sm07"));
            if(HUtil.isNotEmpty(request.getParameter("sm10"))) sysMenu.setSm10(Integer.parseInt(request.getParameter("sm10")));

            this.sysMenuService.updateDataAndUpdateCache(sysMenu);
            return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
        } catch (Exception e) {
            log.info("修改菜单信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }




}
